﻿namespace ETestIT.UI
{
    using System;
    using System.Web.UI.WebControls;
    using Core;
    using Core.Infrastructure;
    using Presentation;
    using Presentation.Storage;
    using SharpArch.Core.PersistenceSupport;

    public partial class Candidats : PageBase<Candidats>
    {
        private IRepository<Candidate> candidateRepository;
        private ISessionProvider sessionStorage;

        private const string Editcandidatekey = "editCandidateKey";

        protected void Page_Load(object sender, EventArgs e)
        {
            candidateAddEdit.Save += CandidateAddEditSave;

            candidateRepository = IoC.Resolve<IRepository<Candidate>>();
            sessionStorage = IoC.Resolve<ISessionProvider>();

            if (!IsPostBack)
                LoadAllCandidats();
        }

        private void LoadAllCandidats()
        {
            gvCandidats.DataSource = candidateRepository.GetAll();
            gvCandidats.DataBind();
        }

        protected void BtnAddNewCandidate_Clicked(object sender, EventArgs e)
        {
            candidateAddEdit.Visible = true;
        }

        protected void LnkEditCommand(object sender, CommandEventArgs e)
        {
            var candId = int.Parse(e.CommandArgument.ToString());

            switch (e.CommandName)
            {
                case "CandidatsEdit":
                    {
                        candidateAddEdit.SetModel(candidateRepository.Get(candId));
                        sessionStorage.Set(Editcandidatekey, candId);
                        candidateAddEdit.Visible = true;
                    }
                    break;
                case "CandidatsDelete":
                    {
                        var candidate = candidateRepository.Get(candId);
                        With.Transaction(()=> candidateRepository.Delete(candidate));
                        LoadAllCandidats();
                    }
                    break;
                case "ViewCandidateResume":
                    {
                        sessionStorage.Set(Const.SelectedCandidateKey, candId);
                        Response.Redirect("~/Resumes.aspx");
                    }
                    break;
            }
        }

        private void CandidateAddEditSave(object sender, EventArgs e)
        {
            var theCandidate = sessionStorage.Contains(Editcandidatekey) ? 
                candidateRepository.Get(sessionStorage.Get<int>(Editcandidatekey)) : 
                new Candidate();

            candidateAddEdit.UpdateModel(theCandidate);
            With.Transaction(()=> 
                candidateRepository.SaveOrUpdate(theCandidate));

            Dialog.Information = "The candidate list was updated successifully";

            LoadAllCandidats();

            if(sessionStorage.Contains(Editcandidatekey))
                sessionStorage.Remove(Editcandidatekey);

            candidateAddEdit.Visible = false;
        }
        protected void gvCandidats_SelectedIndexChanged(object sender, EventArgs eventArgs)
        {

        }
    }
}